Trò chơi Angry Birds trong UNITY Engine
31.667 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class Pig : MonoBehaviour
5 {
6
7 public float Health = 150f;
8 public Sprite SpriteShownWhenHurt;
9 private float ChangeSpriteHealth;
10 // Use this for initialization
11 void Start()
12 {
13 ChangeSpriteHealth = Health - 30f;
14 }
15
16 void OnCollisionEnter2D(Collision2D col)
17 {
18 if (col.gameObject.GetComponent<Rigidbody2D>() == null) return;
19
20 //if we are hit by a bird
21 if (col.gameObject.tag == "Bird")
22 {
23 GetComponent<AudioSource>().Play();
24 Destroy(gameObject);
25 }
26 else //we're hit by something else
27 {
28 //calculate the damage via the hit object velocity
29 float damage = col.gameObject.GetComponent<Rigidbody2D>().velocity.magnitude * 10;
30 Health -= damage;
31 //don't play sound for small damage
32 if (damage >= 10)
33 GetComponent<AudioSource>().Play();
34
35 if (Health < ChangeSpriteHealth)
36 {//change the shown sprite
37 GetComponent<SpriteRenderer>().sprite = SpriteShownWhenHurt;
38 }
39 if (Health <= 0) Destroy(this.gameObject);
40 }
41 }
42
43 //sound found in
44 //https://www.freesound.org/people/yottasounds/sounds/176731/
45 }
2 using System.Collections;
3
4 public class Pig : MonoBehaviour
5 {
6
7 public float Health = 150f;
8 public Sprite SpriteShownWhenHurt;
9 private float ChangeSpriteHealth;
10 // Use this for initialization
11 void Start()
12 {
13 ChangeSpriteHealth = Health - 30f;
14 }
15
16 void OnCollisionEnter2D(Collision2D col)
17 {
18 if (col.gameObject.GetComponent<Rigidbody2D>() == null) return;
19
20 //if we are hit by a bird
21 if (col.gameObject.tag == "Bird")
22 {
23 GetComponent<AudioSource>().Play();
24 Destroy(gameObject);
25 }
26 else //we're hit by something else
27 {
28 //calculate the damage via the hit object velocity
29 float damage = col.gameObject.GetComponent<Rigidbody2D>().velocity.magnitude * 10;
30 Health -= damage;
31 //don't play sound for small damage
32 if (damage >= 10)
33 GetComponent<AudioSource>().Play();
34
35 if (Health < ChangeSpriteHealth)
36 {//change the shown sprite
37 GetComponent<SpriteRenderer>().sprite = SpriteShownWhenHurt;
38 }
39 if (Health <= 0) Destroy(this.gameObject);
40 }
41 }
42
43 //sound found in
44 //https://www.freesound.org/people/yottasounds/sounds/176731/
45 }